<template>
	<view>
		<uni-search-bar :focus="true" v-model="searchValue" @input="onInput">
		</uni-search-bar>
		<!-- 搜索建议列表 -->
		<scroll-view class="sugg-list" scroll-y :style="{height:scrollH+'px'}" v-if="searchResults.length>0">
			<view class="sugg-item" v-for="item in searchResults" :key="item.goods_id" @click="gotoDetail(item.goods_id)">
				<view class="goods-name">{{item.goods_name}}</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</scroll-view>
		<!-- 搜索历史 -->
		<view class="history" v-else>
			<view class="title">
				<text>搜索历史</text>
				<uni-icons type="trash-filled" size="22" color="#999" @click="clearHistory"></uni-icons>
			</view>
			<view class="tag">
				<uni-tag class='tag-item'
				:inverted="true" :text="item" type="success"  
				v-for="(item,i) in historyList" :key="i"
				@click="gotoGoodsList(item)"/>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				searchValue: '',
				time: null,
				searchResults: [],
				scrollH: 0,
				historyList: []
			};
		},
		onLoad() {
			let systemInfo = uni.getSystemInfoSync()
			this.scrollH = systemInfo.windowHeight - 56
			//获取缓存中的历史纪律
			this.historyList = JSON.parse(uni.getStorageSync('searchHistory')||"[]")
		},
		methods: {
			onInput() {
				clearTimeout(this.time)
				this.time = setTimeout(() => {
					console.log(666);
					this.getSearchList()
				}, 500)
			},
			async getSearchList() {
				console.log(typeof this.searchValue);
				if (!this.searchValue) {
					this.searchResults = []
					return
				}
				const {
					data
				} = await uni.$http.get('/api/public/v1/goods/qsearch', {
					query: this.searchValue
				})
				if (data.meta.status !== 200) {
					return uni.showToast({
						title: '数据请求失败！',
						duration: 1500,
						icon: 'none',
					})
				}
				this.searchResults = data.message
			},
			gotoDetail(goods_id) {
				console.log(goods_id);
				this.saveSearchHistory()
			  uni.navigateTo({
			    // 指定详情页面的 URL 地址，并传递 goods_id 参数
			    url: '/subpkg/goods-detail/goods-detail?goods_id=' + goods_id
			  })
				this.searchValue = ''
			},
			saveSearchHistory(){
				this.historyList.unshift(this.searchValue)
				let historySet = new Set(this.historyList)
				this.historyList =[...historySet]
				console.log(this.historyList);
				uni.setStorageSync('searchHistory',JSON.stringify(this.historyList))
			},
			clearHistory(){
				console.log(666);
				this.historyList =[]
				uni.setStorageSync('searchHistory','')
			},
			gotoGoodsList(kw) {
			  uni.navigateTo({
			    url: '/subpkg/goods_list/goods_list?query=' + kw
			  })
			}
		}

	}
</script>

<style lang="scss">
	.sugg-list {
		padding: 10px;
		box-sizing: border-box;
		.sugg-item {
			display: flex;
			margin: 4px;
			justify-content: space-between;
			.goods-name {
				width: 80%;
				// 文字不允许换行（单行文本）
				white-space: nowrap;
				// 溢出部分隐藏
				overflow: hidden;
				// 文本溢出后，使用 ... 代替
				text-overflow: ellipsis;
				margin-right: 3px;
			}
		}
	}
	.history{
		padding: 0 10px;
		.title{
			display: flex;
			justify-content: space-between;
			align-items: center;	
			color: #999;
		}
		.tag{
				padding: 10px 0;
				.tag-item{
					margin: 5px;
				}
		}
	}
	
</style>
